import os

from PIL import Image


def getFileList(dir,Filelist, ext=None):
    """
    获取文件夹及其子文件夹中文件列表
    输入 dir：文件夹根目录
    输入 ext: 扩展名
    返回： 文件路径列表
    """
    newDir = dir
    if os.path.isfile(dir):
        if ext is None:
            Filelist.append(dir)
        else:
            if ext in dir[-3:]:
                Filelist.append(dir)

    elif os.path.isdir(dir):
        for s in os.listdir(dir):
            newDir = os.path.join(dir, s)
            getFileList(newDir, Filelist, ext)

    return Filelist


org_img_folder = "./resources"

# 检索文件
imglist = getFileList(org_img_folder, [], "png")

for imgpath in imglist:
    imgname = os.path.splitext(os.path.basename(imgpath))[0]
    if imgname.endswith("-3"):
        im = Image.open("./resources/" + imgname + ".png")
        out = im.transpose(Image.Transpose.FLIP_TOP_BOTTOM)
        out.save("./resources/" + imgname + ".jpg", False)
    else:
        im = Image.open("./resources/" + imgname + ".png")
        out = im.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
        out.save("./resources/" + imgname + ".png", False)
